import BadRoomStepView from "./BadRoomStepView";
import CollisionHelper from "../../../cfw/collide/CollisionHelper";
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { EventName } from "../../../public/Config";
import BadRoomManager from "../model/BadRoomManager";


const { ccclass, property } = cc._decorator;

@ccclass
export default class BadRoomStepView0 extends BadRoomStepView {

    @property(cc.Node)
    pillow: cc.Node = null;

    @property(cc.Node)
    targetPos: cc.Node = null;

    protected initPos: cc.Vec3 = cc.v3()
    protected model: BadRoomManager;
    start() {
        this.initPos.x = this.pillow.x;
        this.initPos.y = this.pillow.y;
        this.pillow.on(cc.Node.EventType.TOUCH_START, this.touchStart, this)
        this.pillow.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this)
        this.pillow.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this)
        this.pillow.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this)
    }
    touchStart(e: cc.Touch) {

    }

    touchMove(e: cc.Touch) {
        // if (this.clickIndex >= 0) {
        let pos = e.getLocation();
        pos = this.node.convertToNodeSpaceAR(pos)
        this.pillow.x = pos.x;
        this.pillow.y = pos.y;
    }

    touchEnd() {

        let rect1 = this.pillow.getBoundingBox()
        let rect2 = this.targetPos.getBoundingBox()
        if (CollisionHelper.isRectCollideRect(rect1, rect2)) {
            this.pillow.x = this.targetPos.x;
            this.pillow.y = this.targetPos.y;
            this.collideLogic();
        } else {
            this.pillow.x = this.initPos.x;
            this.pillow.y = this.initPos.y;
        }
    }

    collideLogic() {
        GlobalEvent.instance().emit(EventName.UPDATE_SLEEP_STEP, 0)
    }
}
